package com.example.tellwin.home.presenter;

import com.example.tellwin.api.TwjfApi;
import com.example.tellwin.base.RxPresenter;
import com.example.tellwin.base.SimpleCallback;
import com.example.tellwin.home.contract.UploadWorkContract;
import com.example.tellwin.utils.HttpUtils;

import java.io.File;

import javax.inject.Inject;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

/**
 * @author : LinZePeng
 * @date : 2020/11/27
 * @Describe ：
 **/
public class UploadWorkPresenter extends RxPresenter<UploadWorkContract.View> implements UploadWorkContract.Presenter<UploadWorkContract.View>{

    private TwjfApi mApi;

    @Inject
    public UploadWorkPresenter(TwjfApi mApi) {
        this.mApi = mApi;
    }



    @Override
    public void uploadFile(String path) {
        File file = new File(path);
        String name = file.getName();

        RequestBody requestBody = new MultipartBody.Builder()
                .setType(MultipartBody.FORM)
                .addFormDataPart("file", name, RequestBody.create(MediaType.parse("*/*"), file)) // 第一个参数传到服务器的字段名，第二个你自己的文件名，第三个MediaType.parse("*/*")和我们之前说的那个type其实是一样的
                .build();
        mApi.uploadFile(requestBody , new SimpleCallback<String>(mView) {
            @Override
            public void onSuccess(String s) {
                mView.uploadFileResult(s);
            }

            @Override
            public void onFail(String msg) {

            }
        });
    }

    @Override
    public void participateActivity(String activityId ,String url) {
        RequestBody requestBody = HttpUtils.getRequestBody("activityId" ,activityId ,"works" ,url);
        mApi.participateActivity(requestBody, new SimpleCallback<Object>(mView) {
            @Override
            public void onSuccess(Object o) {
                mView.participateActivityResult();
            }

            @Override
            public void onFail(String msg) {

            }
        });
    }
}
